﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.ComponentModel;

namespace WebPALTT.Lib
{
    /*
     * Class: ActionItem_Loop
     *  
     * This class represents a loop action and inherits from @link[ActionItem.cs.htm, ActionItem]. 
     */
    [Serializable()]
    public class ActionItem_Loop : ActionItem
    {
        public override ActionType ActionType { get { return ActionType.Loop; } }

        int mMaxCount;
        int mLoopCount;
        ActionItemCollection mActions;

        /*
         * Name: Properties
         * 
         * /int/ MaxCount:
         * 
         * Gets or sets the number of times this action will loop through its list of actions.
         * This should not be edited once the loop has started (i.e. from a script action inside the loop).
         * 
         * /@link[ActionItemCollection.cs.htm, ActionItemCollection]/ Actions:
         * 
         * Gets the action item collection to loop through.
         */

        /// <summary>
        /// Gets or sets the number of times this action will loop through its list of actions.
        /// </summary>
        [DescriptionAttribute("Number of times to loop.")]
        public int Count { get { return mMaxCount; } set { mMaxCount = value; } }
        [BrowsableAttribute(false)]
        public int LoopCount { get { return mLoopCount; } set { mLoopCount = value; } }

        /// <summary>
        /// Gets or sets the action item collection to loop through.
        /// </summary>
        [BrowsableAttribute(false)]
        public ActionItemCollection Actions { get { return mActions; } }

        public ActionItem_Loop(string name) : this(name, 0, null) { }

        public ActionItem_Loop(string name, int maxCount, ActionItemCollection actions)
            : base(name)
        {
            mMaxCount = maxCount;
            mLoopCount = 0;
            if (actions != null)
                mActions = actions;
            else
                mActions = new ActionItemCollection();
        }

        public ActionItem_Loop(SerializationInfo info, StreamingContext ctxt)
            : base(info, ctxt)
        {
            mMaxCount = (int)info.GetValue("LoopCount", typeof(int));
            mActions = (ActionItemCollection)info.GetValue("Actions", typeof(ActionItemCollection));
            mLoopCount = 0;
        }

        public override void GetObjectData(SerializationInfo info, StreamingContext ctxt)
        {
            base.GetObjectData(info, ctxt);
            info.AddValue("LoopCount", mMaxCount);
            info.AddValue("Actions", mActions);
        }

        public override string[] Validate()
        {
            List<string> errors = new List<string> { };
            if (Name.Equals("Then") || Name.Equals("Else") || Name.Equals("Actions"))
                errors.Add("Error: action[" + Name + "(" + ActionType + ")] is using a reserverd name.");

            if (Actions.Count == 0)
                errors.Add("Warning: action[" + Name + "(" + ActionType + ")]. - Loop action doesn't contain any sub actions");
            foreach (ActionItem action in Actions)
                errors.AddRange(action.Validate());

            return errors.ToArray();
        }

        public override void SetParent(ActionItem parent, int index = 0)
        {
            base.SetParent(parent, index);
            for (int i = 0; i < mActions.Count; i++)
                mActions[i].SetParent(this, i);
        }

        public override ActionItem GetChild(Guid key)
        {
            if (mActions.Contains(key, true))
                return mActions.GetChild(key);
            return null;
        }
    }
}
